<template>
	<div style="width: 600px; text-align: center;">
			<Form ref="pwdValidate" :model="pwdValidate" :rules="pwdrule" label-position="left" :label-width="80">
					<Row type="flex" justify="center">
						<Col span="12">
						<Form-item label="新密码" prop="newpwd" >
							<Input type="password" v-model="pwdValidate.newpwd" placeholder=""></Input>
						</Form-item>
						</Col>
					</Row>
					<Row type="flex" justify="center">
						<Col span="12">
						<Form-item label="确认密码" prop="refpwd" >
							<Input type="password" v-model="pwdValidate.refpwd" placeholder=""></Input>
						</Form-item>
						</Col>
					</Row>
					<Row type="flex" justify="end">
						<Form-item style="margin-right: 20px;">
							<Button type="ghost" @click="PwdReset('pwdValidate')" style="margin-left: 8px">重置</Button>
							<Button type="primary" @click="PwdSubmit('pwdValidate')">提交</Button>
						</Form-item>
					</Row>
			</Form>
</div>
</template>
<script>
	export default {
		data() {

			//输入新密码
			let newpassword = (rule, value, callback) => {
				if(value === '') {
					callback(new Error('请输入密码'));
				} else {
					if(this.pwdValidate.newpwd !== '') {
						// 对第二个密码框单独验证
						this.$refs.pwdValidate.validateField('refpwd');
					}
					callback();
				}
			};
			//再次确认
			let refpassword = (rule, value, callback) => {
				if(value === '') {
					callback(new Error('请再次输入密码'));
				} else if(value !== this.pwdValidate.newpwd) {
					callback(new Error('两次输入密码不一致!'));
				} else {
					callback();
				}
			};

			return {
				pwdValidate: {
					oldpwd: '',
					newpwd: '',
					refpwd: ''
				},
				//修改密码校验
				pwdrule: {
					newpwd: [{
						// required: true,
						validator: newpassword,
						trigger: 'blur'
					}, {
						type: 'string',
						min: 5,
						message: '密码长度不能小于5位',
						trigger: 'blur'
					}],
					refpwd: [{
						// required: true,
						validator: refpassword,
						trigger: 'blur'
					}]
				}
			}
		},
		methods: {

			PwdSubmit(name) {
				let that = this;
				this.$refs[name].validate((valid) => {
					if(valid) {
						this.$http.post('http://192.168.1.126:8084/updatePassword ', {
							newPassword: this.pwdValidate.newpwd,
							surePassword: this.pwdValidate.refpwd
						}).then(function(response) {

							let isOk = response.data;
							console.info 

(isOk)

							if(isOk.flag) {
								that.$Message.success(isOk.message);
							} else {

								that.$Message.error(isOk.message);
							}

						}, response => {

						});

						that.$refs[name].resetFields();
					} else {
						that.$Message.error('表单验证失败!');
					}
				})
			},
			PwdReset(name) {
				this.$refs[name].resetFields();
			}
		}
	}
</script>
<style>
	
</style>
